﻿<?php
set_time_limit(0);
error_reporting(0);
include("function.php");
$phpbb_root_path = '../';
$filename=$_FILES['file']['name'];
$type=$_GET['type'];
$sid=$_GET['sid'];
$url=$_POST['url'];  
if (function_exists('curl_init'))
{
	$snatch_system = 'curl';
}

if(!is_dir("temp"))mkdir("temp");
if(!is_dir("temppath"))mkdir("temppath");
if(!is_dir("download"))mkdir("download");
$ds=rand(0000000001,9999999999).'.jar';
$copy_fail = true;



function get($name)
{
    return substr($name,-4,4);
}
$boddy="";
if($type=="")
{

    $error=true;
    $boddy=$boddy."<p>请选择上传类型</p>";

}
else
{
    if($type=="url")
    {
       if($url!="")
       {
            $url=trim($url);
            if(get($url)==".jar" || get($url)==".zip")
            {
                
                if(@fopen($url,"r"))
                {
                //文件存在
                    $boddy=$boddy.'正在上传文件...<br/>';
                    if ($snatch_system == 'curl')
                    {
                        $ch = curl_init($url);
                        $fp = fopen("temp/".$ds, 'w');
                        curl_setopt($ch, CURLOPT_FILE, $fp);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        curl_exec($ch);
                        $curl_info =  curl_getinfo($ch);
                        curl_close($ch);
                        fclose($fp);
                        $copy_fail = false;
                        
                    }
                    else
                    {
                        if (!copy($url, "temp/".$ds))
                        {
                            $boddy=$boddy.'文件上传失败! <a href="'.$url.'">'.$url.'</a><br/>请点击链接查看是否有误<br/>';
                            $copy_fail = true;
                        }
                        else
                        {
                            $copy_fail = false;
                        
                        }
                    }


                }

                else
                {
                    $copy_fail = true;
                    $boddy=$boddy."远程文件不存在<br/>";
                     //文件不存在
                }
            }
            else
            {
                $copy_fail=true;
                $boddy=$boddy."URL地址无效<br/>注:URL地址必须为完整的URL文件地址，以.jar或.zip结尾<br/>";
            }
            
       }
       else
       {
            $copy_fail=true;
            $boddy=$boddy."请输入URL地址<br/>注:URL地址必须为完整的URL文件地址，以.jar或.zip结尾<br/>";
       }
       
       

       
    }
    else if($type=="file")
    {
        if($filename!="")
        {
            if(get($filename)==".jar" || get($filename)==".zip")
            {
                
                @move_uploaded_file($_FILES['file']['tmp_name'],"temp/$ds");
                $copy_fail =false;
                
            
            }
            else
            {
                $copy_fail =true;
                $boddy=$boddy."$filename 不是jar或zip文件<br/>";

            }
        }
        else
        {
            $boddy=$boddy."请选择一个jar或zip文件上传<br/>";
        }
    }
    
    if ($copy_fail == false)
    {
        $boddy=$boddy.'正在处理文件...<br/>';
        require_once('pclzip.lib.php');
        $temppath="temppath/".str_replace(".jar","",$ds);
        if(!is_dir($temppath))mkdir($temppath);
        $zip = new PclZip("temp/".$ds);
        $file_list= $zip->listContent();            
        $list = $zip->extract(PCLZIP_OPT_PATH, $temppath );


        $issms=false;
        for($i=0;$i<sizeof($list);$i++)
        {
            //$a = $zip->extract(PCLZIP_OPT_BY_NAME,$file_list[$i], PCLZIP_OPT_EXTRACT_AS_STRING);

            //$a = $zip->extract(PCLZIP_OPT_BY_NAME,$file_list[$i]['filename'], PCLZIP_OPT_EXTRACT_AS_STRING);
            $path=$list[$i]['filename'];
            if(strpos($path,"/META-INF/MANIFEST.MF"))
            {
                $mf=file_get_contents($path);
                
                if(!strpos(" ".$mf,"MIDlet-Delete-Confirm"))
                {
                    $mf=preg_replace (  
                        "/MIDlet-Vendor: (.*)/i",   
                        "MIDlet-Vendor: agrj.cn",$mf); 
                    $f=fopen($path,"w");                    
                    fwrite($f,"MIDlet-Delete-Confirm: 确认删除吗？手机访问 wap.qt3g.com 还有更多好玩的免费游戏等你下载！\r\n".$mf);
                    fclose($f);
                }
            }
            $tempcontent =file_get_contents($path);

            if(strpos($tempcontent,"javax/microedition/io/Connector") && strpos($tempcontent,"javax/wireless/messaging/MessageConnection"))
            {
                $boddy=$boddy."发短信文件:".str_replace("./".$temppath."/","",$path)."<br/>";
                $issms=true;
                //替换
                $f=fopen($path,"wb");                    
                fwrite($f,str_replace("javax/microedition/io/Connector","javay/microedition/io/Connector",$tempcontent));
                fclose($f);
                //插入javay包
                xCopy("template/",$temppath,1);
                unset($tempcontent);
            }
            
        }
        if($issms==true)
        {
            if(file_exists($temppath."/META-INF/MANIFEST.MF"))
            {
                $archive = new PclZip('download/'.$ds);
                $archive->create($temppath,PCLZIP_OPT_REMOVE_PATH, $temppath);
                
                $mf=file_get_contents($temppath."/META-INF/MANIFEST.MF");

                $mf = str_replace("\n\n","\r\n",$mf);
                $mf .= "MIDlet-Jar-Size: ".filesize('download/'.$ds)."\r\nMIDlet-Jar-URL: ".$ds."";
                $f=fopen("download/".str_replace(".jar",".jad",$ds),"w");
                fwrite($f,$mf."\r\n");
                fclose($f);  
                $boddy=$boddy."破解成功!游戏出现短信收费提示属正常现象，游戏已经破解，实在不放心可以在离线模式下玩。<br/><a href=\"download/$ds\">JAR下载".(filesize('download/'.$ds)/1000)."kb</a>|<a href=\"download/".str_replace(".jar",".jad",$ds)."\">JAD下载</a><br/>";                          
            }
            else
            {
                $boddy=$boddy."缺少MANIFEST.MF文件，此文件不是有效的手机应用程序<br/>";
            }
        
        }
        else $boddy=$boddy."没找到短信收费代码，不能破解此游戏<br/>";
        

    }
    else
    {
        $boddy=$boddy."文件上传失败!<br/>";
    } 
    
    
}
                $f=fopen("ab.txt","w");
                fwrite($f,$boddy);
                fclose($f);
header("Location: index3.asp?sid=$sid"); 
?>